<template>
	<view>
		<form @submit="formSubmit">
			<view class='personal-data' :style="colorStyle">
				<view class='list'>
					<view class='item acea-row row-between-wrapper'>
						<view>头像</view>
						<view class="avatar-box" @click.stop='uploadpic(1)'>
							<image :src="userInfo.avatar"></image>
						</view>
					</view>
					<view class='item acea-row row-between-wrapper'>
						<view>资质</view>
						<view class="avatar-box" @click.stop='uploadpic(2)'>
							<image :src="userInfo.qualifications"></image>
						</view>
					</view>
					<view class='item acea-row row-between-wrapper'>
						<view>授权书</view>
						<view class="avatar-box" @click.stop='uploadpic(3)'>
							<image :src="userInfo.auth_certificate"></image>
						</view>
					</view>
					<view class='item acea-row row-between-wrapper'>
						<view>商户名称</view>
						<view class='input'><input type='text' name='merchant_name' :value='userInfo.merchant_name'></input>
						</view>
					</view>
					<view class='item acea-row row-between-wrapper'>
						<view>联系方式</view>
						<view class='input'><input type='text' name='link_tel' :value='userInfo.link_tel'></input>
						</view>
					</view>
					<view class='item acea-row row-between-wrapper'>
						<view>商户分类</view>
						<view class='input'>
							<uni-data-select
									v-model="value"
									:localdata="options"
									@change="getValue"
							></uni-data-select>
						</view>
					</view>
					<view class='item acea-row row-between-wrapper'>
						<view>地址</view>
						<view class='input'><input type='text' name='address' :value='userInfo.address'></input>
						</view>
					</view>
				</view>

				<button class='modifyBnt bg-color' formType="submit">保存修改</button>
			</view>
		</form>
		<canvas canvas-id="canvas" v-if="canvasStatus"
			:style="{width: canvasWidth + 'px', height: canvasHeight + 'px',position: 'absolute',left:'-100000px',top:'-100000px'}"></canvas>

	</view>
</template>

<script>
	import {
		getUserInfo,
		userEdit
	} from '@/api/merchant.js';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	import colors from '@/mixins/color.js';
	export default {
		components: {

		},
		mixins: [colors],
		data() {
			return {
				userInfo: {},
				canvasWidth: "",
				canvasHeight: "",
				canvasStatus: false,
				fileSizeString: '',
				options: {},
				value:0
			};
		},
		computed: mapGetters(['isLogin']),
		watch: {
			isLogin: {
				handler: function(newV, oldV) {
					if (newV) {
						this.getUserInfo();
					}
				},
				deep: true
			}
		},
		onLoad() {
			if (this.isLogin) {
				this.getUserInfo();
			} else {
				toLogin();
			}
		},
		methods: {
			getValue(index) {
				this.userInfo.classify_id = index
			},

			/**
			 * 获取商户详情
			 */
			getUserInfo: function() {
				let that = this;
				let user = that.$Cache.get('new_user_info')
				getUserInfo(JSON.parse(user).merchant.id).then(res => {
					that.$set(that, 'userInfo', res.data.merchant);
					that.$set(that, 'options', res.data.classify);
					that.$set(that, 'value', res.data.merchant.classify_id);
				});
			},

			/**
			 * 上传文件
			 * 
			 */
			uploadpic(type) {
				let that = this;
				this.canvasStatus = true
				that.$util.uploadImageChange('upload/image', (res) => {
					if (that.userInfo !== undefined && type === 1) {
						that.userInfo.avatar = res.data.url;
					}
					if (that.userInfo !== undefined && type === 2) {
						that.userInfo.qualifications = res.data.url;
					}
					if (that.userInfo !== undefined && type === 3) {
						that.userInfo.auth_certificate = res.data.url;
					}
					this.canvasStatus = false
				}, (res) => {
					this.canvasStatus = false
				}, (res) => {
					this.canvasWidth = res.w
					this.canvasHeight = res.h
				});
			},

			/**
			 * 提交修改
			 */
			formSubmit: function(e) {
				let that = this,
					value = e.detail.value;
				if (!value.merchant_name && !that.userInfo.merchant_name) return that.$util.Tips({
					title: '商户名称不能为空'
				});
				if (!value.avatar && !that.userInfo.avatar) return that.$util.Tips({
					title: '请上传商户头像'
				});
				if (!value.qualifications && !that.userInfo.qualifications) return that.$util.Tips({
					title: '请上传商户资质'
				});
				if (!value.link_tel && !that.userInfo.link_tel) return that.$util.Tips({
					title: '联系方式不能为空'
				});
				if (!value.classify_id && !that.userInfo.classify_id) return that.$util.Tips({
					title: '商户分类不能为空'
				});
				if (!value.address && !that.userInfo.address) return that.$util.Tips({
					title: '商户地址不能为空'
				});
				value.id = this.userInfo.id;
				value.avatar = this.userInfo.avatar;
				value.qualifications = this.userInfo.qualifications;
				value.auth_certificate = this.userInfo.auth_certificate;
				value.classify_id = parseInt(this.userInfo.classify_id);
				userEdit(value).then(res => {
					this.$util.Tips({
						title: res.msg
					});
					that.reload();
				}).catch(msg => {
					return that.$util.Tips({
						title: msg || '保存失败，您并没有修改'
					}, {
						tab: 3,
						url: 1
					});
				});
			},

			reload() {
				// 页面重载
				const pages = getCurrentPages()
				// 声明一个pages使用getCurrentPages方法
				const curPage = pages[pages.length - 1]
				// 声明一个当前页面
				curPage.onLoad(curPage.options) // 传入参数
			},
		}
	}
</script>

<style scoped lang="scss">
	.cartcolor {
		color: var(--view-theme);
		border: 1px solid var(--view-theme);
	}

	.personal-data .wrapper {
		margin: 10rpx 0;
		background-color: #fff;
		padding: 36rpx 30rpx 13rpx 30rpx;
	}

	.personal-data .wrapper .title {
		margin-bottom: 30rpx;
		font-size: 32rpx;
		color: #282828;
	}

	.personal-data .wrapper .wrapList .item {
		width: 690rpx;
		height: 160rpx;
		background-color: #f8f8f8;
		border-radius: 20rpx;
		margin-bottom: 22rpx;
		padding: 0 30rpx;
		position: relative;
		border: 2rpx solid #f8f8f8;
		box-sizing: border-box;
	}

	.personal-data .wrapper .wrapList .item.on {
		border-color: #e93323;
		border-radius: 20rpx;
		background-image: url("");
		background-size: 100% 100%;
		background-color: #fff9f9;
		background-repeat: no-repeat;
	}

	.personal-data .wrapper .wrapList .item .picTxt {
		width: 445rpx;
	}

	.personal-data .wrapper .wrapList .item .picTxt .pictrue {
		width: 96rpx;
		height: 96rpx;
		position: relative;
	}

	.personal-data .wrapper .wrapList .item .picTxt .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 50%;
	}

	.personal-data .wrapper .wrapList .item .picTxt .pictrue .alter {
		width: 30rpx;
		height: 30rpx;
		border-radius: 50%;
		position: absolute;
		bottom: 0;
		right: 0;
	}

	.personal-data .wrapper .wrapList .item .picTxt .text {
		width: 325rpx;
	}

	.personal-data .wrapper .wrapList .item .picTxt .text .name {
		width: 100%;
		font-size: 30rpx;
		color: #282828;
	}

	.personal-data .wrapper .wrapList .item .picTxt .text .phone {
		font-size: 24rpx;
		color: #999;
		margin-top: 10rpx;
	}

	.personal-data .wrapper .wrapList .item .bnt {
		font-size: 24rpx;
		background-color: #fff;
		border-radius: 27rpx;
		width: 140rpx;
		height: 54rpx;
		border: 2rpx solid #e93323;
	}

	.personal-data .wrapper .wrapList .item .currentBnt {
		position: absolute;
		right: 0;
		top: 0;
		font-size: 26rpx;
		background-color: rgba(233, 51, 35, 0.1);
		width: 140rpx;
		height: 48rpx;
		border-radius: 0 20rpx 0 20rpx;
	}

	.personal-data .list {
		margin-top: 15rpx;
		background-color: #fff;
	}

	.personal-data .list .item {
		padding: 30rpx 30rpx 30rpx 0;
		border-bottom: 1rpx solid #f2f2f2;
		margin-left: 30rpx;
		font-size: 32rpx;
		color: #282828;
	}

	.personal-data .list .item .phone {
		width: 160rpx;
		height: 56rpx;
		font-size: 24rpx;
		color: #fff;
		line-height: 56rpx;
		border-radius: 32rpx
	}

	.personal-data .list .item .pictrue {
		width: 88rpx;
		height: 88rpx;
	}

	.personal-data .list .item .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 50%;
	}

	.personal-data .list .item .input {
		width: 415rpx;
		text-align: right;
		color: #868686;
	}

	.personal-data .list .item .input .id {
		width: 365rpx;
	}

	.personal-data .list .item .input .iconfont {
		font-size: 35rpx;
	}

	.personal-data .modifyBnt {
		font-size: 32rpx;
		color: #fff;
		width: 690rpx;
		height: 90rpx;
		border-radius: 50rpx;
		text-align: center;
		line-height: 90rpx;
		margin: 76rpx auto 0 auto;
	}

	.personal-data .logOut {
		font-size: 32rpx;
		text-align: center;
		width: 690rpx;
		height: 90rpx;
		border-radius: 45rpx;
		margin: 30rpx auto 0 auto;
	}

	.avatar-box {
		width: 96rpx;
		height: 96rpx;

		image {
			width: 100%;
			height: 100%;
			border-radius: 50%;
		}
	}
</style>
